function loginSave() {
	localStorage['username'] = $('username-input').value;
	localStorage['password'] = $('password-input').value;
	chrome.extension.getBackgroundPage().delicious = delicious;
	delicious.scheduleLoading();
	loginRestore();
}
function loginRestore() {
	$('username-input').value = localStorage['username'] || "Enter your name";
	$('password-input').value = localStorage['password']
			|| "Enter yout password";
	loginEdited();
}

function tryLogin() {
	$('loadingtip').removeClassName('disabled');
	delicious = getDeliciousBookmarSystem($('username-input').value,
			$('password-input').value);
	loginId = delicious.tryLogin();
}

function doLogin() {
	$('loadingtip').addClassName('disabled');
}

function cancelLogin() {
	loginId = 0;
	$('loadingtip').addClassName('disabled');
	delicious.cancelLogin();
}

function schedLoginEdited() {
	editJob.schedule();
}

function loginEdited() {
	editJob.run();
}

function createStartDragBookmark(div, bookmark) {
	return function(e) {
		var dt = ev.originalEvent.dataTransfer;
		dt.setData("bookmark", bookmark);
		return true;
	}
}

window.addEventListener('load', function() {
	editJob = new Job(function() {
				if ($('username-input').value == localStorage['username']
						&& $('password-input').value == localStorage['password']) {
					$('restore-button').disabled = $('save-button').disabled = true;
					$('restore-button').addClassName('disabled');
					$('save-button').addClassName('disabled');
				} else {
					$('restore-button').disabled = $('save-button').disabled = false;
					$('restore-button').removeClassName('disabled');
					$('save-button').removeClassName('disabled');
				}
				$('login-button').removeClassName('disabled');
			});

	subPub.subscribe({
				name : 'delicious/login',
				callback : (function(result) {
					try {
						if (result.loginId == window.loginId) {
							$$('.input-item.result').each(function(el) {
										el.innerHTML = 'Waiting...';
									});

							$('loadingtip').addClassName('disabled');
							if (result.result == 'success') {
								loadResults();
								$('succtip').removeClassName('disabled');
								setTimeout(function() {
											$('succtip')
													.addClassName('disabled');
										}, 3000);

							} else if (result.result == 'failed') {
								$$('.input-item.result').each(function(el) {
											el.innerHTML = 'Failed...';
										});
								$('failtip').removeClassName('disabled');
								setTimeout(function() {
											$('failtip')
													.addClassName('disabled');
										}, 3000);
							} else if (result.result == 'timeout') {
								$$('.input-item.result').each(function(el) {
											el.innerHTML = 'Failed...';
										});
								$('timeouttip').removeClassName('disabled');
								setTimeout(function() {
											$('timeouttip')
													.addClassName('disabled');
										}, 3000);
							}
						}
					} catch (e) {
						console.dir(e.stack || e);
					}
				}).bind(this)
			});
	loginRestore();
	loadResults();
}, false);